package edu.hawaii.duedates;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Test;

/**
 * Tests the functions of the ItemDue object.
 * 
 * @author Tyler Wolff and Scheller Sanchez
 */
public class TestItemDue {

  /**
   * Tests the constructor and accessor/mutator methods of the ItemDue object.
   */
  @Test
  public void testNewItemDue() {

    ItemDue itemDue = new ItemDue("UH Manoa", "book title", "book", "11-20-08");

    assertEquals("Testing accessors lib", "UH Manoa", itemDue.getItemLib());
    assertEquals("Testing accessors name", "book title", itemDue.getItemName());
    assertEquals("Testing accessors type", "book", itemDue.getItemType());
    assertEquals("Testing accessors date", "11-20-08", itemDue.getItemDueDate());

    // changing values
    itemDue.setItemLib("UH Manoa1");
    itemDue.setItemName("Book");
    itemDue.setItemType("video");
    itemDue.setItemDueDate("11-21-08");

    assertEquals("Testing mutator lib", "UH Manoa1", itemDue.getItemLib());
    assertEquals("Testing mutator name", "Book", itemDue.getItemName());
    assertEquals("Testing mutator type", "video", itemDue.getItemType());
    assertEquals("Testing mutator date", "11-21-08", itemDue.getItemDueDate());

  }

  /**
   * Tests the toString method of the ItemDue object.
   */
  @Test
  public void testToString() {

    // whiteSpace created and used just like libraries would.
    String whiteSpace = "                                                  ";
    ItemDue itemDue = new ItemDue("UH Manoa", "Title".concat(whiteSpace), "book", "11-20-08");

    assertEquals("Testing toString", "|UH Manoa               |Title                         "
        + "                 ...|book                |11-20-08  |", itemDue.toString());
  }
  
  /**
   * Tests the equals method of the ItemDue class.
   */
  @Test
  public void testEquals() {
    ItemDue itemDue = new ItemDue("UH Manoa3", "book title3", "book3", "12-20-08");
    ItemDue itemDue2 = new ItemDue("UH Manoa3", "book title3", "book3", "12-20-08");
    ItemDue itemDue3 = new ItemDue("UH Manoa3", "book title3", "book3", "12-20-08");
    
    assertEquals("Testing Reflexitive", itemDue, itemDue);
    assertEquals("Testing Symmetry", itemDue, itemDue2);
    assertEquals("Testing Symmetry", itemDue2, itemDue);
    
    assertEquals("Testing Transitivity", itemDue, itemDue2);
    assertEquals("Testing Transitivity", itemDue2, itemDue3);
    assertEquals("Testing Transitivity", itemDue, itemDue3);
  }
  
  /**
   * Testing unequal objects in the equals method of the ItemDue class.
   */
  @Test
  public void testNotEquals() {
    ItemDue itemDue = new ItemDue("UH Manoa4", "book title4", "book4", "10-21-08");
    ItemDue itemDue2 = new ItemDue("Hawaii State Library", "book title4", "book4", "09-20-08");
    
    assertFalse("Testing Inequality", itemDue.equals(itemDue2));
  }
   
  /**
   * Testing null in the equals method of the ItemDue class.
   */
  @Test
  public void testNullEquals() {
    ItemDue itemDue = new ItemDue("UH Manoa5", "book title5", "book5", "08-20-08");
    String nullString = null;
    
    assertFalse("Testing Null Reference", itemDue.equals(nullString));
  }
  
  /**
   * Testing the hashCode method of the ItemDue class.
   */
  @Test
  public void testHashCode() {
    ItemDue itemDue = new ItemDue("UH Manoa6", "book title6", "book6", "10-20-08");
    ItemDue itemDue2 = new ItemDue("UH Manoa6", "book title6", "book6", "10-20-08");
    
    assertEquals("Testing hashCode equality", itemDue.hashCode(), itemDue2.hashCode());
    
    ItemDue itemDue3 = new ItemDue("blankLib", "blanktitle", "blankbook", "blank date");
    itemDue3.setItemDueDate(null);
    itemDue3.setItemLib(null);
    itemDue3.setItemName(null);
    itemDue3.setItemType(null);
    
    assertNotSame("Testing null hashcode", itemDue.hashCode(), itemDue3.hashCode());
  }
  
  /**
   * Tests comparators in ItemDue class.
   */
  @Test
  public void testComparators() {
    List<ItemDue> allItems = new ArrayList<ItemDue>();
    ItemDue itemDue = new ItemDue("1", "book title7", "book7", "10-21-08");
    ItemDue itemDue2 = new ItemDue("2", "book title8", "book8", "08-30-08");
    
    allItems.add(itemDue2);
    allItems.add(itemDue);
    
    //sort by library
    Collections.sort(allItems, ItemDue.libraryComparator);
    assertEquals("First item should be itemDue", itemDue, allItems.get(0));
    
    //sort by duedate
    Collections.sort(allItems, ItemDue.dueDateComparator);
    assertEquals("First item should be itemDue2", itemDue2, allItems.get(0));
    
    //adding item that should turn up first
    ItemDue itemDue3 = new ItemDue("3", "A will come first", "book9", "08-31-08");
    allItems.add(itemDue3);
    
    //sort by name
    Collections.sort(allItems, ItemDue.itemNameComparator);
    assertEquals("First item should be itemDue3", itemDue3, allItems.get(0));
  }

}